package org.example.software.controller;

import org.example.software.entity.Tools;
import org.example.software.service.PaintService;
import org.example.software.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.example.software.entity.Paint;
import java.util.List;

@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/paint")
public class PaintCotroller {

    @Autowired
    private PaintService paintService;

    // 添加漆
    @PostMapping("/add")
    public String addPaint(@RequestHeader("Authorization") String token, @RequestBody Paint paint) {
        if (!JwtUtil.isAdmin(token)) {
            return "权限不足";
        }
        paintService.addPaint(paint);
        return "漆添加成功";
    }

    // 删除漆
    @DeleteMapping("/delete/{id}")
    public String dropPaintById(@RequestHeader("Authorization") String token, @PathVariable int id) {
        if (!JwtUtil.isAdmin(token)) {
            return "权限不足";
        }
        paintService.deletePaintById(id);
        return "漆删除成功";
    }

    // 修改漆
    @PutMapping("/update")
    public String updatePaint(@RequestHeader("Authorization") String token, @RequestBody Paint paint) {
        if (!JwtUtil.isAdmin(token)) {
            return "权限不足";
        }
        paintService.updatePaint(paint);
        return "漆信息更新成功";
    }

    // 查找所有漆
    @GetMapping("/all")
    public List<Paint> getAllPaints() {
        return paintService.getAllPaints();
    }

    // 根据工具ID查找
    @GetMapping("/{id}")
    public List<Paint> getPaintById(@PathVariable int id) {
        return paintService.getPaintById(id);
    }

    //根据制造商查找
    @GetMapping("/manufacturerName/{manufacturerName}")
    public List<Paint> getPaintByName(@PathVariable String manufacturerName) {
        return paintService.getPaintByName(manufacturerName);
    }

    // 根据颜色查找
    @GetMapping("/color/{color}")
    public List<Paint> getPaintByColor(@PathVariable String color) {
        return paintService.getPaintByColor(color);
    }

    // 根据容量查找
    @GetMapping("/capacity/{capacity}")
    public List<Paint> getPaintByCapacity(@PathVariable String capacity) {
        return paintService.getPaintByCapacity(capacity);
    }

}
